<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends BaseController {
	//cate 配置类型1:唯一变量（接口地址）2:可变变量
    public function sys_config($cate=1,$keywords,$p){
        $m=M('Config');
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$map['name|value|en_name'] = array('like','%'.$keywords.'%');
		$map['cate']=$cate;
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$return['count']=$count;
		$return['list']=$list;
		$return['show']=$show;
		return $return;

    }
	//cate 配置类型1:唯一变量（接口地址）2:可变变量
    public function sys_configErp($cate=1,$keywords,$p){
        $m=M('ConfigErp');
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$map['name|value|en_name'] = array('like','%'.$keywords.'%');
		$map['cate']=$cate;
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$return['count']=$count;
		$return['list']=$list;
		$return['show']=$show;
		return $return;

    }
	public function baseconfig(){
		$keywords=I('get.keywords');
		$p=I('p')?I('p'):1;
		$return=$this->sys_config(2,$keywords,$p);
		$this->assign("count",$return['count']);
		$this->assign("list",$return['list']);
		$this->assign("cate",2);
		$this->assign('page',$return['show']);// 赋值分页输出
		$this->display();
	}
	public function interfaces(){
		$keywords=I('get.keywords');
		$p=I('p')?I('p'):1;
		$return=$this->sys_config(1,$keywords,$p);
		$this->assign("count",$return['count']);
		$this->assign("list",$return['list']);
		$this->assign("cate",1);
		$this->assign('page',$return['show']);// 赋值分页输出
		$this->display('baseconfig');
	}
	public function baseconfigErp(){
		$keywords=I('get.keywords');
		$p=I('p')?I('p'):1;
		$return=$this->sys_configErp(2,$keywords,$p);
		$this->assign("count",$return['count']);
		$this->assign("list",$return['list']);
		$this->assign("cate",2);
		$this->assign('page',$return['show']);// 赋值分页输出
		$this->display();
	}
	public function interfacesErp(){
		$keywords=I('get.keywords');
		$p=I('p')?I('p'):1;
		$return=$this->sys_configErp(1,$keywords,$p);
		$this->assign("count",$return['count']);
		$this->assign("list",$return['list']);
		$this->assign("cate",1);
		$this->assign('page',$return['show']);// 赋值分页输出
		$this->display('baseconfigErp');
	}
	//添加
	function add_configs(){
			$m=M('Config');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$count=$m->where(array('en_name'=>$param['en_name']))->count();
			if($count){
				$this->error("此英文变量名称已经存在!");
				die;
			}
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_config(){
		if(IS_POST){
			$m=M('Config');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_config(){
		$id=trim(I('id'));
		$m=M('Config');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_config(){
		$id=trim(I('id'));
		$m=M('Config');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//添加
	function add_configsErp(){
			$m=M('ConfigErp');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$count=$m->where(array('en_name'=>$param['en_name']))->count();
			if($count){
				$this->error("此英文变量名称已经存在!");
				die;
			}
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_configErp(){
		if(IS_POST){
			$m=M('ConfigErp');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_configErp(){
		$id=trim(I('id'));
		$m=M('ConfigErp');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_configErp(){
		$id=trim(I('id'));
		$m=M('ConfigErp');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}